﻿ <?php
ignore_user_abort(TRUE);
require_once("./interface.php");
require_once("./utils.php");
require_once("./interface.php");
require_once("./TechnicFunc.php");
require_once("./SoldierFunc.php");
require_once("./HotelFunc.php");
require_once("./OfficeFunc.php");
require_once("./MarketFunc.php");
require_once("./UnionFunc.php");
require_once("./StoreFunc.php");
require_once("./TroopFunc.php");
require_once("./global.php");
require_once("./report.php");

//header("Content-type: text/html; charset=utf8"); 
//mysql_connect()函数

$con = mysql_connect("127.0.0.1","root","111111");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sanguo1", $con);//链接数据库

//触发前线清空相关表数据
mysql_query("DELETE from sys_user_cityresource ");
mysql_query("DELETE from rank_user ");
mysql_query("DELETE from rank_hero ");
mysql_query("DELETE from rank_hero_affairs ");
mysql_query("DELETE from rank_hero_bravery ");
mysql_query("DELETE from rank_hero_wisdom ");
//===============统计城池人口总数
$citys=sql_fetch_into_arrays("select * from sys_city order by uid desc");//用户城池表
$j=0;
while($j<=10000)
  {
$city_cid=$citys['cid'][$j];
$city_uid=$citys['uid'][$j];
$citys_r=sql_fetch_into_arrays("select * from mem_city_resource where cid='$city_cid'");//mem_city_resource 取数
$citys_r_people=$citys_r['people'][0];
mysql_query("INSERT INTO sys_user_cityresource (cid,uid,people) 
VALUES ('$city_cid', '$city_uid', '$citys_r_people')");
//echo $city_cid."----".$city_uid."----".$citys_r_people."<br/>";
  $j=$j+1;
  }
//==================================================玩家排行
$users=sql_fetch_into_arrays("select * from sys_user where passtype!='npc' order by nobility desc,prestige desc ");//用户表
$juewei=sql_fetch_into_arrays("select * from cfg_nobility order by id ");//爵位表
$unions=sql_fetch_into_arrays("select * from sys_union order by id ");//union_id联盟ID
$citys=sql_fetch_into_arrays("select * from sys_city order by uid desc");//用户城池表
$i=0;
$k=1;
while($users['name'][$i]!="")
  {
$jwid=$users['nobility'][$i] ;//要显示用户的爵位ID
$jw=$juewei['name'][$jwid];//对应的爵位名称
$union_id=$users['union_id'][$i];//要显示用户的联盟ID
$union_name=sql_fetch_into_arrays("select * from sys_union where id='$union_id' ");//union_id联盟ID
$uid=$users['uid'][$i];
$city_count=sql_fetch_into_arrays("select count(*) from sys_city where uid='$uid' ");//玩家城池数目
$city_r_u=sql_fetch_into_arrays("SELECT SUM(people) AS peoples FROM sys_user_cityresource where uid='$uid' order by peoples desc ");
$r_u_name=$users['name'][$i];
$r_u_lm=$union_name['name'][0];
$r_u_sw=$users['prestige'][$i];
$r_u_citys=$city_count['count(*)'][0];
$r_u_peoples=$city_r_u['peoples'][0];

mysql_query("INSERT INTO rank_user (rank,uid,name,city,people,prestige,nobility,`union`) 
VALUES ('$k','$uid','$r_u_name','$r_u_citys','$r_u_peoples','$r_u_sw','$jw','$r_u_lm')");

//mysql_query("INSERT INTO rank_hero (rank,uid,name,nobility,level,affairs,bravery,wisdom) 
//VALUES ('$k', '$hero_hid', '$hero_name','$hero_junzhu','$hero_level','$sum_affairs','$sum_bravery','$sum_wisdom')");

//mysql_query("INSERT INTO rank_user (rank,uid,name,union,nobility,prestige,city,people) 
//VALUES ('$k','$uid','$r_u_name', '$r_u_un','$jw','$r_u_sw','$r_u_citys','$r_u_peoples')");
// echo $k."----".$r_u_name."----".$jw."----".$r_u_lm."----".$r_u_sw."----".$r_u_citys."----".$r_u_peoples."<br>";
  $i=$i+1;
  $k++;
  }

$d=0;
$ranks=sql_fetch_into_arrays("select * from rank_user order by rank ");//君主名称
while($ranks['rank'][$d]!="")
  {

$rank_s_u=$ranks['rank'][$d];
$uid_s_u=$ranks['uid'][$d];
mysql_query("UPDATE sys_user SET rank = $rank_s_u WHERE uid='$uid_s_u'");

//echo $rank_s_u."----".$uid_s_u."<br>";
$d=$d+1;
  }
 //==============================英雄排行 按等级排行
$heros=sql_fetch_into_arrays("select * from sys_city_hero where command_base!=0 order by level desc,command_base desc ");//将领表
/*echo "<table width='600' border='1' cellpadding='0' cellspacing='0' bordercolor='#666666'>
  <tr>
    <td width='60' height='20'><div align='center'><strong>排名</strong></div></td> 
    <td height='20'><div align='center'><strong>将领</strong></div></td>
    <td height='20'><div align='center'><strong>君主</strong></div></td>
	<td height='20'><div align='center'><strong>等级</strong></div></td>
    <td height='20'><div align='center'><strong>统帅</strong></div></td>
    <td height='20'><div align='center'><strong>内政</strong></div></td>
    <td height='20'><div align='center'><strong>勇武</strong></div></td>
    <td height='20'><div align='center'><strong>智谋</strong></div></td>
  </tr>";*/
$a=0;
$b=1;
while($heros['name'][$a]!="")
  {
$heros_uid=$heros['uid'][$a];
$junzhu=sql_fetch_into_arrays("select * from sys_user where uid=$heros_uid ");//君主名称
$sum_command=$heros['command_base'][$a]+$heros['command_add_on'][$a]+$heros['level'][$a];//统帅
$sum_affairs=$heros['affairs_base'][$a]+$heros['affairs_add_on'][$a]+$heros['affairs_add'][$a];//内政
$sum_bravery=$heros['bravery_base'][$a]+$heros['bravery_add_on'][$a]+$heros['bravery_add'][$a];//勇武
$sum_wisdom=$heros['wisdom_base'][$a]+$heros['wisdom_add_on'][$a]+$heros['wisdom_add_on'][$a];//智谋
$hero_hid=$heros['hid'][$a];
$hero_name=$heros['name'][$a];
$hero_junzhu=$junzhu['name'][0];
$hero_level=$heros['level'][$a];
mysql_query("INSERT INTO rank_hero (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$b', '$hero_hid', '$hero_name','$hero_junzhu','$hero_level','$sum_affairs','$sum_bravery','$sum_wisdom')");


	
  $a=$a+1;
  $b=$b+1;
  }


//=============将领按内政、勇武、智谋排行
$heros_a=sql_fetch_into_arrays("select * from rank_hero order by affairs desc,level desc");//将领排行表 按内政排行
$heros_b=sql_fetch_into_arrays("select * from rank_hero order by bravery desc,level desc ");//将领排行表 按勇武排行
$heros_w=sql_fetch_into_arrays("select * from rank_hero order by wisdom desc,level desc ");//将领排行表 按智谋排行
$i=0;
$k=1;
while($heros_a['name'][$i]!="")
  {
//内政排行变量定义
$h_hid_a=$heros_a['hid'][$i];
$h_name_a=$heros_a['name'][$i];
$h_user_a=$heros_a['user'][$i];
$h_level_a=$heros_a['level'][$i];
$h_affairs_a=$heros_a['affairs'][$i];
$h_ubravery_a=$heros_a['bravery'][$i];
$h_wisdom_a=$heros_a['wisdom'][$i];
//勇武排行变量定义
$h_hid_b=$heros_b['hid'][$i];
$h_name_b=$heros_b['name'][$i];
$h_user_b=$heros_b['user'][$i];
$h_level_b=$heros_b['level'][$i];
$h_affairs_b=$heros_b['affairs'][$i];
$h_ubravery_b=$heros_b['bravery'][$i];
$h_wisdom_b=$heros_b['wisdom'][$i];
//智谋排行变量定义
$h_hid_w=$heros_w['hid'][$i];
$h_name_w=$heros_w['name'][$i];
$h_user_w=$heros_w['user'][$i];
$h_level_w=$heros_w['level'][$i];
$h_affairs_w=$heros_w['affairs'][$i];
$h_ubravery_w=$heros_w['bravery'][$i];
$h_wisdom_w=$heros_w['wisdom'][$i];
//写入rank_hero_affairs表
mysql_query("INSERT INTO rank_hero_affairs (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_a', '$h_name_a','$h_user_a','$h_level_a','$h_affairs_a','$h_ubravery_a','$h_wisdom_a')");
//写入rank_hero_bravery表
mysql_query("INSERT INTO rank_hero_bravery (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_b', '$h_name_b','$h_user_b','$h_level_b','$h_affairs_b','$h_ubravery_b','$h_wisdom_b')");
//写入rank_hero_wisdom表
mysql_query("INSERT INTO rank_hero_wisdom (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_w', '$h_name_w','$h_user_w','$h_level_w','$h_affairs_w','$h_ubravery_w','$h_wisdom_w')");
  $i=$i+1;
  $k=$k+1;
  }
  

 mysql_close($con);
?>



